The present invention relates to locking of an object in a multi-thread program, and more specifically, to a method and system for switching a locking mode of an object in a multi-thread program.
With development of computer hardware technologies, multi-core chips on which several processor cores are integrated and processing systems using such multi-core chips, such as computers or the like, have been developed. Additionally, in order to speed up execution of a program, a Simultaneous Multi-Threading (SMT) technology was proposed, which renders that each processor core may support a plurality of threads. Therefore, one processing system may execute many threads simultaneously. In order to utilize a processing ability of such processing system, programs running on the processing system are also multi-threaded.
When a multi-thread program is executed, there are a plurality of threads simultaneously, which access various objects in the program, such as functions, statement blocks, data structures, or the like, according to needs, and release these objects when they are not used. A situation where multiple threads access a same object simultaneously (called contentious access hereinafter) may occur, thereby a collision occurs. To cope with the contentious access to the object, a locking mechanism may be applied to the object.
Conventionally, the locking mechanism applied to the object may be classified, according to a locking granularity, into a coarse-granularity locking mode and a fine-granularity locking mode, where the coarse-granularity locking mode is a mode of performing locking in a relatively coarse granularity of, for example, an object or the like, and the fine-granularity locking mode is a mode of performing locking in a relatively fine granularity of, for example, an field of the object or the like. According to a locking level, the locking mechanism applied to the object may be classified into a single-level locking mode and a multi-level locking mode, where the single-level locking mode is a mode of performing locking at one level, for example, a locking mode of performing locking at a level of an object, and the multi-level locking mode is a mode of performing locking at several levels, for example, a mode of performing locking at both the level of an object and the level of the filed of the object, respectively.
The following code segment 1 gives an example of an object (Class “MusicRack”) which employs the single-level locking mode, where “Lock MusicRackLock” defines a lock for the object (object-level lock).
[Code segment 1] Class MusicRack{  Lock MusicRackLock;  int MusicCount; List topSelection;...  List MusicTrack1;  List MusicTrack2;  ...}
The following code segment 2 gives an example of an object (Class “MusicRack”) which employs the multi-level locking mode, where “ReadWriteLock MusicRackLock” defines a lock for the object (object-level lock), and “Lock MusicTrack1Lock” and “Lock MusicTrack2Lock” define locks (field-level locks) for fields “List MusicTrack1” and “List MusicTrack2” of the object.
[Code segment 2] Class MusicRack{  ReadWriteLock MusicRackLock;  int MusicCount; List topSelection;...  Lock MusicTrack1Lock;  List MusicTrack1;  Lock MusicTrack2Lock;  List MusicTrack2;  ...}
Different locking modes may cause different performances. The single-level locking mode is easy to implement and has a short code path, and thus may enable a program to have a greater execution speed in an environment where there are few contentious accesses, but it has a low efficiency in an environment where the contentious accesses are relatively frequent due to its low parallelism degree. On the contrary, the multi-level locking mode may improve the parallelism degree by lowering the locking level, i.e. reducing a locking range, and thus may obtain a greater processing speed in the environment where the contentious accesses are relatively frequent, but it has a low efficiency in the environment where there are few contentious accesses, because it has a long code path and a deadlock problem may arise.
Conventionally, the locking mode of the object in the multi-thread program is set when the program is written, and when the program is executed, operations are performed according to the locking mode. However, different multi-thread programs have different runtime behaviors, thus the locking mode of the object in each program has to be designed according to an actual condition of the program, which renders the program design work to be very cumbersome. Additionally, it is very difficult to predict runtime behaviors of the multi-thread program, thus it is difficult to set a most suitable locking mode for the object therein at the time of design. Moreover, even for the same multi-thread program, its runtime behaviors vary dynamically, thus the locking mode set at the time of design may become inapplicable at different execution stages of the program.